class Solution {
    public int maximumCostSubstring(String s, String chars, int[] vals) {
        // 先赋值花费
        int[] cost = new int[26];
        for (int i = 0; i < 26; i++)
            cost[i] = i + 1;
        for (int i = 0; i < chars.length(); i++) {
            // === String 定位用charAt
            cost[(int)chars.charAt(i) - (int)('a')] = vals[i];
        }
        int[] dp = new int[100010];
        int ans = 0;
        // 以chars[i]结尾字串的最大开销
        for (int i = 1; i <= s.length(); i++) {
            int index = (int)s.charAt(i - 1) - (int)'a';
            dp[i] = Math.max(cost[index],dp[i-1] + cost[index]);
            ans = Math.max(ans,dp[i]);
        }
        return ans;
    }
}